package com.easyui.controller;

import com.easyui.pojo.Menu;
import com.easyui.pojo.User;
import com.easyui.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/menu.html")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping(params = "act=page")
    public String page() {
        return "/menu";
    }

    @RequestMapping(params = "act=tree")
    @ResponseBody
    public List<Menu> menus() {
//        Menu m1 = new Menu(1, "酒店管理", "", null);
//        Menu m11 = new Menu(11, "索引管理", "page.jsp", 1);
//        Menu m12 = new Menu(12, "用户管理", "page2.jsp", 1);
//        Menu m13 = new Menu(13, "酒店列表", "page2.jsp", 1);
//        List<Menu> childrenList1 = new ArrayList<>();
//        childrenList1.add(m11);
//        childrenList1.add(m12);
//        childrenList1.add(m13);
//        m1.setChildren(childrenList1);
//
//        Menu m2 = new Menu(2, "航班管理", "", null);
//        Menu m21 = new Menu(21, "订单管理", "", 2);
//        List<Menu> childrenList2 = new ArrayList<>();
//        childrenList2.add(m21);
//        m2.setChildren(childrenList2);
//
//        List<Menu> menus = new ArrayList<>();
//        menus.add(m1);
//        menus.add(m2);
        return menuService.getTreeMenu();
    }


    @RequestMapping(params = "act=go_edit")
    public String goEdit(Integer id, Model model) {
        if (id != null) {
            Menu menu = menuService.getMenuById(id);
            model.addAttribute("menu", menu);
        }
        return "/menu_add";
    }

    @RequestMapping(params = "act=edit")
    @ResponseBody
    public Map<String, Object> edit(Menu menu) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (menu.getId() == null) {
                menuService.addMenu(menu);
            } else {
                menuService.updateMenu(menu);
            }
            result.put("success", true);
            result.put("message", "编辑成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequestMapping(params = "act=delete")
    @ResponseBody
    public Map<String, Object> delete(Integer[] ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            menuService.deleteMenu(ids);
            result.put("success", true);
            result.put("message", "删除成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }


}
